package com.android.syssetting

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.syssetting.databinding.ActivityBrightnessSettingBinding
import com.android.syssetting.databinding.ActivitySystemSettingBinding
import com.android.syssetting.models.BrightnessViewModel
import com.android.syssetting.models.SysInfoModel
import com.camera.base.Log.YLogImpl
import com.camera.base.util.ToastUtil
import com.camera.base.v.BaseActivity
import com.camera.routemodule.RouterPath
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.path.brightness_activity)
class BrightnessActivity: BaseActivity<ActivityBrightnessSettingBinding, BrightnessViewModel>() {
    val TAG = BrightnessActivity::class.java.simpleName

    override val viewModelClass: Class<BrightnessViewModel>
        get() = BrightnessViewModel::class.java
    override fun getLayoutId(): Int {
        return R.layout.activity_brightness_setting
    }

    override fun getStatusBarColor(): Int {
        return R.color.title_background_color
    }

    override fun loadData() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                intent.data = Uri.parse("package:" + this.packageName)
                this.startActivity(intent)
                ToastUtil.showLong("请授予应用权限");
                finish()
            } else {
                // 在此处执行亮度设置操作
                val currentBrightness = getCurrentBrightness()
                YLogImpl.i(TAG, "currentBrightness = $currentBrightness")
                mBinding.sbActivitySysSettingVolume.progress = currentBrightness
            }
        } else {
            val currentBrightness = getCurrentBrightness()
            YLogImpl.i(TAG, "currentBrightness = $currentBrightness")
            mBinding.sbActivitySysSettingVolume.progress = currentBrightness
        }
    }

    override fun initView() {
        mBinding.sbActivitySysSettingVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                // 根据进度值设置设备亮度
                setBrightness(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}

            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })


    }

    // 获取当前设备亮度值的方法
    private fun getCurrentBrightness(): Int {
        return Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS)
    }

    // 设置设备亮度的方法
    private fun setBrightness(brightness: Int) {
        // 调用系统API设置亮度值
        val layoutParams = window.attributes
        layoutParams.screenBrightness = brightness.toFloat() / 255
        window.attributes = layoutParams
        // 保存设置的亮度值
        Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness)
    }
}